//stc-header: C:\Users\xxx\.platformio\platforms\intel_mcs51\examples\stc-header\src
//header files: https://github.com/darconeous/sdcc/tree/master/device/include/mcs51

// #include <reg51.h>       // old header from SDCC
// #include "STC8Fxx.h"     // Official header from STC-ISP for STC8Fxx / STC8Axx
// #include "STC12C5Axx.h"  // Official header from STC-ISP for STC10Fxx / STC11Fxx / STC12C5Axx / STC12C52xx
// #include "STC12C20xx.h"  // Official header from STC-ISP for STC12C20xx
// #include "STC12C54xx.h"  // Official header from STC-ISP for STC12C54xx
// #include "STC12C56xx.h"  // Official header from STC-ISP for STC12C56xx
// #include "STC15Fxx.h"    // Official header from STC-ISP for STC15Wxx / STC15Fxx
// #include "STC15F104E.h"  // Official header from STC-ISP for STC15F204EA / STC15F104E
// #include "STC89xx.h" // Official header from STC-ISP for STC89xx / STC90xx
// #include "STC90C5xAD.h"  // Official header from STC-ISP for STC90C5xAD

// #include <8051.h>
#include <8052.h>
#include "misc.h"

//#include "say_hello.h"
//#include "show_num.h"
//#include "keyborad.h"
//#include "timer.h"
//#include "daad.h"
//#include "send_receive.h"
// #include "lcd1602.h"
// #include "eeprom_wr.h"
#include "eeprom.h"
//#include "time_show_in_led1602.h"
// #include "DS18B20.h"
//#include "IRReceiver.h"
// #include "L298N.h"
#include "UART.h"
#include "MPU6050.h"

void begin(void);
void end(void);

void UART_TEST();
void MPU6050_TEST();

void main()
{
    begin();
    //显示HELLO.
    //sayhello();
    //流水灯展示（中断）
    //showNum();
    //点亮LED
    //keybord_show_led();
    //键盘显示数字
    //keybord_show_num();
    //矩阵键盘显示
    //keybord_show_4x4();
    //时钟显示
    //timer_go();
    //DA展示
    //da_show();
    //ad/da
    //ad_da_show();
    //串口和电脑发送接收文件
    //send_receive();
    //1602液晶显示
    //lcd1602_show();
    //读写eeprom
    //writeAndRead();
    //i2c_eeprom();
    //
    //show_time_in_lcd1062();
    //lcd1602_show_number(13579);

    //DS18B20_temper_change();
    //DS18B20_ShowTemp();

    //IRReceiver_Test();
    //使用IR要引用#include "IRReceiver.h" 引用中断函数
    //L298N_Test();

    //UART_TEST();

    //MPU6050_TEST();

    UART_Init();

    MPU6050_Init();
    while (1)
    {
        //P1 = 0b10101011;
        /* code */
        MPU6050_TEST();

        // unsigned char d = MPU6050_WHOAMI();
        // UART_SendIntData(d);
        // UART_SendOneByte('\r');
        // UART_SendOneByte('\n');
        // delay(1000);
        // if (d > 0)
        // {
        //     P1 = d;
        // }
    }

    //end();
}

void MPU6050_TEST()
{
    //UART_SendString("MPU6050_GYRO", 12);
    // while (1)
    // {
    //     P1 = MPU6050_WHOAMI();
    // }

    int *p = MPU6050_GYRO();
    for (char i = 0; i < 3; i++)
    {
        UART_SendIntData(p[i]);
        UART_SendOneByte('\r');
        UART_SendOneByte('\n');
    }
    UART_SendString("====\r\n", 6);
    delay(10);
}

void UART_Receive_Back(char c)
{
    P1 = c;
}

void UART_TEST()
{
    UART_Init();
    UART_SET_Interrupt4_Callback(UART_Receive_Back);

    __code unsigned char str[] = "Hello,From 51MCU!\r\n";
    unsigned char len = sizeof(str) / sizeof(str[0]);
    //char a = '1';
    while (1)
    {
        //     UART_SendIntData(12345);
        //     UART_SendOneByte('\r');
        //     UART_SendOneByte('\n');
        //     UART_SendIntData(-9856);
        //     UART_SendOneByte('\r');
        //     UART_SendOneByte('\n');

        delay(1000);
        UART_SendString(str, len);
        UART_SendOneByte('A');
        UART_SendOneByte('\r');
        UART_SendOneByte('\n');
        UART_SendOneByte(0);
        UART_SendOneByte('B');
        // UART_SendOneByte(a);
        // a++;
        // if (a > 'z')
        // {
        //     a = '1';
        // }
    }
}

void begin(void)
{
    beep(100);
    delay(50);
    beep(100);
}

void end(void)
{
}